package com.zj.leetcode.easy.tree;

/**
 * @program: algorithm
 * @description: 合并二叉树
 * 617 merge-two-binary-trees
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class MergeTwoBinaryTrees {
    public static void main(String[] args) {
        Solution solution = new MergeTwoBinaryTrees().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        /**
         * 首先对于二叉树的先中后序遍历的会写
         * @param root1
         * @param root2
         * @return
         */
        public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {

            if (root1 == null) {
                return root2;
            }
            if (root2 == null) {
                return root1;
            }
            // 先合并根节点
            root1.val += root2.val;
            // 再递归合并左右子树
            root1.left = mergeTrees(root1.left, root2.left);
            root1.right = mergeTrees(root1.right, root2.right);
            return root1;
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}
